package com.liutao;

import java.io.File;
import java.io.IOException;
import java.lang.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.io.PrintWriter;

import net.sf.json.JSONArray;

import com.duansky.DataPreProcess;
public class small_world {
	private int V,c;
	private double threshold;
	public static void main(String[] args) throws IOException {
		
		small_world a=new small_world();
		a.gen(null);
	}
	public small_world(){
		this.V=100;
		this.c=4;
		this.threshold=0.7;
	}
	public void setV(int v){
		this.V=v;
	}
	public void setC(int c){
		this.c=c;
	}
	public void setS(double s){
		this.threshold=s;
	}
	public int[][] readText(String filePath)throws IOException{
		String baseFilePath = filePath + "liutao"+File.separator+"data";
		String txt_path=baseFilePath+File.separator+"data.txt";
		 File file=new File(txt_path);
		 int[][] edge=new int[V][c];
		 for(int i=0;i<V;i++)
			 for(int j=0;j<c;j++)
			 {
				 edge[i][j]=-1;
			 }
		 Scanner in = new Scanner(file);
		 while (in.hasNextLine()) {
            String str = in.nextLine();
            String strr = str.trim();
            String[] abc = strr.split("\t");
            int rows= Integer.parseInt(abc[0]);
            int target = Integer.parseInt(abc[1]);
            for(int i=0;i<c;i++){
           	 if(edge[rows][i]==-1){
           		 edge[rows][i]=target;
           	     break;
           	 }
            }
        }
		 return edge;
	}
	public JSONArray diffusion(int[][] edge)throws IOException{
        double beta=threshold;
        JSONArray jn=new JSONArray();
		 Set<Integer> T=new HashSet<Integer>();
		  Set<Integer> cur=new HashSet<Integer>();
		  cur.add(0);
		  T.add(0);
		  while(!cur.isEmpty()){
			  //copy the cur
			  Set<Integer> temp=new HashSet<Integer>();
			  for(int aa:cur){
				  temp.add(aa);
			  }
			  cur.clear();
			  Iterator<Integer> it=temp.iterator();
			  while(it.hasNext()){
				  int point=it.next();
				  for(int i=0;i<c;i++){
					  if(!T.contains(edge[point][i])&&edge[point][i]!=-1){
						  double alpha=Math.random();
						  //System.out.print(alpha+" ");
						  if(alpha<beta){
							  cur.add(edge[point][i]);
							  T.add(edge[point][i]);
						  }
					  }
				  }
			  }
			  JSONArray temp_jn=new JSONArray();
			  for(int a:T){
				  temp_jn.add(Integer.toString(a));
				 // System.out.print(a+"  ");
			  } 
			  jn.add(temp_jn);
			 // System.out.print("\n");
		  }
		  return jn;
	}
	
	public  void gen(String filePath) throws IOException{
		String baseFilePath = filePath + "liutao"+File.separator+"data";
	  	String txt_path=baseFilePath+File.separator+"data.txt";
	  	String json_path=baseFilePath+File.separator+"data.json";
		double beta=0.2;
	    int[][] edge=new int[V][c];
	    for(int i=0;i<V;i++)
	    	for(int j=0;j<c;j++){
	    		edge[i][j]=judge(V,i,-(c/2)+j);
	    	}
	   for(int i=0;i<V;i++){
		   for(int j=0;j<c;j++){
			   if(edge[i][j]>i){
				   int k=(int)(Math.random()*V);
				   if(!small_world.has_value(edge[i], k)){
					   double alpha=Math.random();
					   if(alpha<beta){
						   int temp=edge[i][j];
						   edge[i][j]=k;
						   for(int ii=0;ii<c;ii++){
							   if(edge[temp][ii]==i)
								   edge[temp][ii]=-1;
						   }
					   }
				   }
			   }
		   }
	   }
	 File file=new File(txt_path);
	 PrintWriter  files=new PrintWriter(file);
	 for(int i=0;i<V;i++){
		 for(int j=0;j<c;j++){
			 if(edge[i][j]!=-1){
				 String str1=""+i;
				 String str2=""+edge[i][j];
				 String str3=""+1;
				 files.write(str1+"\t"+str2+"\t"+str3+"\n");
				// System.out.print(str1+"\t"+str2+"\t"+str3+"\n");
			 }
		 }
		
	 }
  	 files.close();
  	 DataPreProcess dp=new DataPreProcess();
  	 dp.translate_txt_to_json(txt_path,json_path);
	}
static	int judge(int N,int i,int n){
		if((i+n)<0)
			return N+i+n;
		if(n>-1)
		return (i+n+1)%N;
		return i+n;
	}
static boolean has_value(int[] array,int n){
	int lenth=array.length;
	for(int i=0;i<lenth;i++){
		if(n==array[i])
			return true;
	}
	return false;
}
}
